package com.idroid.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.NumberPicker;
import android.widget.TimePicker;

import java.lang.reflect.Field;

/**
 * Created by oushuhua
 */
public class CustomTimePicker extends TimePicker {
    public CustomTimePicker(Context context) {
        super(context);
        init();
    }

    public CustomTimePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomTimePicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    public void init() {
        getNumberPicker(this);
    }


    public void getNumberPicker(TimePicker timePicker) {
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$id");
            Field fieldHour = clazz.getField("hour");
            fieldHour.setAccessible(true);
            int hourId = fieldHour.getInt(null);
            NumberPicker hourNumberPicker = (NumberPicker) timePicker.findViewById(hourId);
            setDividerColor(hourNumberPicker);

            Field fieldminute = clazz.getField("minute");
            fieldminute.setAccessible(true);
            int minuteId = fieldminute.getInt(null);
            NumberPicker minuteNumberPicker = (NumberPicker) timePicker.findViewById(minuteId);
            setDividerColor(minuteNumberPicker);

//            //更改冒号颜色
//            Field fieldDivider=clazz.getField("divider");
//            fieldDivider.setAccessible(true);
//            int dividerId=fieldDivider.getInt(null);
//            TextView textView_devider=(TextView)timePicker.findViewById(dividerId);
//            textView_devider.setTextColor(Color.RED);
//
//            //更改hour字体(未实现)
//            Field fieldTextInput=clazz.getField("numberpicker_input");
//            fieldTextInput.setAccessible(true);
//            int textInputId=fieldHour.getInt(null);
//
//            TextView textView_hour=(TextView)hourNumberPicker.findViewById(textInputId);
//            textView_hour.setTextColor(Color.YELLOW);
//
//            TextView textView_minute=(TextView)minuteNumberPicker.findViewById(textInputId);
//            textView_minute.setTextColor(Color.YELLOW);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void setDividerColor(NumberPicker picker) {
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            //Log.v("zhangbo", "pf:" + pf.getName() + " type :" + pf.getGenericType());
            if (pf.getName().equals("mSelectionDivider"))//能找到这个域 （分割线视图)
            {
                Log.v("zhangbo", "find......mSelectionDivider");
                pf.setAccessible(true);
                try {
                    ColorDrawable colorDrawable = new ColorDrawable();
                    colorDrawable.setColor(0x00000000);
                    pf.set(picker, colorDrawable);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }/*else if(pf.getName().equals("mInputText")){  // 这个找不到~~~(当前Number)
                Log.v("zhangbo", "find......mInputText");
                pf.setAccessible(true);
                try {
                    EditText editText = new EditText(picker.getContext());
                    editText.setTextColor(0xff0000);
                    pf.set(picker,editText);
                }catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            }*/

        }
    }
}
